package com.mdc.mansion.core.modules.dishes.impl;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.common.restaurant.RestaurantCommon;
import com.mdc.mansion.core.common.translate.TranslateLanguage;
import com.mdc.mansion.core.common.translate.TranslateService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesComboItemService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesComboService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.core.modules.dishes.dto.RestaurantDishesComboDTO;
import com.mdc.mansion.core.modules.dishes.dto.RestaurantDishesComboItemDTO;
import com.mdc.mansion.core.modules.order.RestaurantOrderComboItemService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.dishes.dao.RestaurantDishesComboDao;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishes;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesCombo;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesComboItem;
import com.mdc.mansion.dataaccess.modules.dishes.entity.mo.RestaurantDishesComboMO;
import com.mdc.mansion.dataaccess.modules.order.entity.RestaurantOrderComboItem;

/**
 * 餐厅菜品套餐信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class RestaurantDishesComboServiceImpl extends CrudService<RestaurantDishesComboDao, RestaurantDishesCombo> implements RestaurantDishesComboService {
    @Autowired
    private RestaurantDishesComboItemService restaurantDishesComboItemService;
    @Autowired
    private RestaurantDishesService restaurantDishesService;
    @Autowired
    private TranslateService translateService;
    @Autowired
    private RestaurantOrderComboItemService restaurantOrderComboItemService;
    
    
    @Override
    public List<RestaurantDishesComboMO> findListWithItem(String restaurantId) {
        List<RestaurantDishesComboMO> restaurantDishesComboMOList = new ArrayList<>();
        // 查询所有有效的套餐
        RestaurantDishesCombo paramRestaurantDishesCombo = new RestaurantDishesCombo();
        paramRestaurantDishesCombo.setRestaurantId(restaurantId);
        paramRestaurantDishesCombo.setDelFlag("0");
        Page<RestaurantDishesCombo> page = new Page<RestaurantDishesCombo>(-1);
        page.setOrderBy("sort_num");
        paramRestaurantDishesCombo.setPage(page);
        List<RestaurantDishesCombo> restaurantDishesComboList = this.findList(paramRestaurantDishesCombo);
        if(restaurantDishesComboList==null || restaurantDishesComboList.size() == 0){
            return restaurantDishesComboMOList;
        }
        for(RestaurantDishesCombo restaurantDishesCombo : restaurantDishesComboList){
            RestaurantDishesComboMO restaurantDishesComboMO = new RestaurantDishesComboMO();
            BeanUtils.copyProperties(restaurantDishesCombo, restaurantDishesComboMO);
            String dishesComboId = restaurantDishesCombo.getId();
            RestaurantDishesComboItem paramRestaurantDishesComboItem = new RestaurantDishesComboItem();
            paramRestaurantDishesComboItem.setRestaurantDishesComboId(dishesComboId);
            paramRestaurantDishesComboItem.setRestaurantId(restaurantId);
            paramRestaurantDishesComboItem.setDelFlag("0");
            paramRestaurantDishesComboItem.setPage(new Page<RestaurantDishesComboItem>(-1));
            List<RestaurantDishesComboItem> dishesComboItemList = restaurantDishesComboItemService.findList(paramRestaurantDishesComboItem);
            restaurantDishesComboMO.setDishesComboItemList(dishesComboItemList);
            restaurantDishesComboMOList.add(restaurantDishesComboMO);
        }
        return restaurantDishesComboMOList;
        
    }
    @Transactional
    @Override
    public void saveDishesCombo(RestaurantDishesComboMO restaurantDishesComboMO) {
        String id = restaurantDishesComboMO.getId(); // 套餐ID
        String restaurantId = ContextHolder.currentRestaurantId();
        restaurantDishesComboMO.setRestaurantId(restaurantId);
        String name = restaurantDishesComboMO.getName();
        String language = restaurantDishesComboMO.getLanguage();
        if(language!=null&&!"".equals(language)){
            String chinaName = translateService.translate(name, TranslateLanguage.CHINA);
            String englishName = translateService.translate(name, TranslateLanguage.ENGLISH);
            String indonesiaName = translateService.translate(name, TranslateLanguage.INDONESIA);
            restaurantDishesComboMO.setName(indonesiaName);
            restaurantDishesComboMO.setEnglishName(englishName);
            restaurantDishesComboMO.setChineseName(chinaName);
        }
        List<RestaurantDishesComboItem> dishesComboItemList = restaurantDishesComboMO.getDishesComboItemList();
        // 调用翻译接口进行翻译
         if(id==null||"".equals(id)){// 新增 
             this.save(restaurantDishesComboMO);
             id  = restaurantDishesComboMO.getId();
         }else{
        	 // 修改
             this.updateSelective(restaurantDishesComboMO);
             // 先删除原来的item
             RestaurantDishesComboItem paramRestaurantDishesComboItem = new RestaurantDishesComboItem();
             paramRestaurantDishesComboItem.setRestaurantDishesComboId(id);
             paramRestaurantDishesComboItem.setRestaurantId(restaurantId);
             paramRestaurantDishesComboItem.setDelFlag("0");
             List<RestaurantDishesComboItem> oldDishesComboItemList = restaurantDishesComboItemService.findList(paramRestaurantDishesComboItem);
             if(oldDishesComboItemList!=null && oldDishesComboItemList.size()>0){
                 for(RestaurantDishesComboItem restaurantDishesComboItem : oldDishesComboItemList){
                     restaurantDishesComboItemService.delete(restaurantDishesComboItem);
                 }
             }
         }
         // 添加新的套餐菜品
         if(dishesComboItemList!=null&&dishesComboItemList.size()>0){
             for(RestaurantDishesComboItem restaurantDishesComboItem : dishesComboItemList){
                 restaurantDishesComboItem.setRestaurantDishesComboId(id);
                 restaurantDishesComboItem.setRestaurantId(restaurantId);
                 restaurantDishesComboItemService.save(restaurantDishesComboItem);
             }
         } 
    }
    @Transactional
    @Override
    public void delete(String restaurantDishesComboId) {
        //验证套餐是否存在 如果存在 查询出明细
        RestaurantDishesCombo restaurantDishesCombo = this.get(restaurantDishesComboId);
        if(restaurantDishesCombo == null){
            throw new BusinessException(I18nUtil.getMessage("dishes.combo.not.exists"));
        }
        // 获取明细
        RestaurantDishesComboItem paramRestaurantDishesComboItem = new RestaurantDishesComboItem();
        paramRestaurantDishesComboItem.setRestaurantDishesComboId(restaurantDishesComboId);
        paramRestaurantDishesComboItem.setRestaurantId(ContextHolder.currentRestaurantId());
        paramRestaurantDishesComboItem.setDelFlag("0");
        paramRestaurantDishesComboItem.setPage(new Page<RestaurantDishesComboItem>(-1));
        List<RestaurantDishesComboItem> dishesComboItemList = restaurantDishesComboItemService.findList(paramRestaurantDishesComboItem);
        if(dishesComboItemList!=null&&dishesComboItemList.size()>0){
            for(RestaurantDishesComboItem restaurantDishesComboItem : dishesComboItemList){
                restaurantDishesComboItemService.delete(restaurantDishesComboItem);
            }
        }
        this.delete(restaurantDishesCombo);
    }
    @Override
    public RestaurantDishesComboDTO findById(String id) {
        RestaurantDishesCombo restaurantDishesCombo = this.get(id);
        if(restaurantDishesCombo==null){return null;}
        RestaurantDishesComboDTO dishesComboDTO=new RestaurantDishesComboDTO();
        BeanUtils.copyProperties(restaurantDishesCombo,dishesComboDTO);
        RestaurantDishesComboItem comboItem = new RestaurantDishesComboItem();
        comboItem.setRestaurantDishesComboId(id);
        comboItem.setPage(new Page<RestaurantDishesComboItem>(-1));
        List<RestaurantDishesComboItem> comboItems = restaurantDishesComboItemService.findList(comboItem);
        List<RestaurantDishesComboItemDTO> dishesComboItemDTOs= Lists.newArrayList();
        dishesComboDTO.setRestaurantDishesComboItemDTOs(dishesComboItemDTOs);
        if(CollectionUtils.isEmpty(comboItems)){return dishesComboDTO;}
        for(RestaurantDishesComboItem item:comboItems){
            RestaurantDishes restaurantDishes = restaurantDishesService.get(item.getRestaurantDishesId());
            RestaurantDishesComboItemDTO comboItemDTO=new RestaurantDishesComboItemDTO();
            BeanUtils.copyProperties(restaurantDishes,comboItemDTO);
            BeanUtils.copyProperties(item,comboItemDTO);
            dishesComboItemDTOs.add(comboItemDTO);
        }
        return dishesComboDTO;
    }
}
